local hx__qionglan = fk.CreateSkill {
  name = "hx__qionglan",
  tags = { Skill.Compulsory },
}


hx__qionglan:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local skills = {}
  local mark = player:getTableMark("hx__qionglan_skills")
  if mark == nil then
    mark = {}
  end
  for _, p in ipairs(player.room:getOtherPlayers(player, false)) do
    if p.kingdom == "wu" then
      for _, s in ipairs(p:getSkillNameList()) do
        if not player:hasSkill(s, true) then
          table.insert(skills, s)
          table.insert(mark, s)
        end
      end
    end
  end
  room:setPlayerMark(player, "hx__qionglan_skills", mark)
  local allskills = 0
  for _, p in ipairs(room:getAlivePlayers()) do
    if p.kingdom == "wu" then
      allskills = #p:getSkillNameList() + allskills
    end
  end
  room:setPlayerMark(player, "@hx__qionglan", allskills)
  if allskills >= 36 then
    room:notifySkillInvoked(player, hx__qionglan.name)
    player:broadcastSkillInvoke(hx__qionglan.name)
    room:delay(2000)
    if player.role == "lord" or player.role == "loyalist" then
      player.room:gameOver("lord+loyalist")
    else
      player.room:gameOver(player.role)
    end
  end
  if #skills > 0 then
    room:handleAddLoseSkills(player, table.concat(skills, "|"))
  end
end)

local spec = {
  mute = true,
  priority = 1,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__qionglan.name) and
    table.find(player.room:getOtherPlayers(player), function (p)
      return p.kingdom == "wu" and
        table.find(p:getSkillNameList(), function(s)
          return not player:hasSkill(s, true)
        end) ~= nil
    end) then
      return true
    end
    if player:hasSkill("hx__qionglan") and
    table.find(player.room:getAlivePlayers(), function (p)
      return p.kingdom == "wu" and
        table.find(p:getSkillNameList(), function(s)
          return true
        end) ~= nil
    end) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = {}
    local mark = player:getTableMark("hx__qionglan_skills")
    if mark == nil then
      mark = {}
    end
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p.kingdom == "wu" then
        for _, s in ipairs(p:getSkillNameList()) do
          if not player:hasSkill(s, true) then
            table.insert(skills, s)
            table.insert(mark, s)
          end
        end
      end
    end
    room:setPlayerMark(player, "hx__qionglan_skills", mark)
    local allskills = 0
    for _, p in ipairs(room:getAlivePlayers()) do
      if p.kingdom == "wu" then
        allskills = #p:getSkillNameList() + allskills
      end
    end
    room:setPlayerMark(player, "@hx__qionglan", allskills)
    if allskills >= 36 then
      room:notifySkillInvoked(player, hx__qionglan.name)
      player:broadcastSkillInvoke(hx__qionglan.name)
      room:delay(2000)
      if player.role == "lord" or player.role == "loyalist" then
        player.room:gameOver("lord+loyalist")
      else
        player.room:gameOver(player.role)
      end
    end
    if #skills > 0 then
      room:handleAddLoseSkills(player, table.concat(skills, "|"))
    end
  end,
}

local spec2 = {
  mute = true,
  is_delay_effect = true,
  priority = 2,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__qionglan.name) then
      return player:getTableMark("hx__qionglan_skills") ~= nil
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = {}
    local lost_skills = {}
    local mark = player:getTableMark("hx__qionglan_skills")
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p.kingdom == "wu" then
        for _, s in ipairs(p:getSkillNameList()) do
          table.insert(skills, s)
        end
      end
    end
    for  _ ,sk in ipairs(mark) do
      if not table.contains(skills, sk) then
        table.insert(lost_skills, sk)
      end
    end
    if #lost_skills > 0 then
      room:handleAddLoseSkills(player, "-" .. table.concat(lost_skills, "|-"))
    end
  end,
}

hx__qionglan:addEffect(fk.EventAcquireSkill, spec)

hx__qionglan:addEffect(fk.EventAcquireSkill, spec2)


return hx__qionglan
